package com.example.calcite.xml;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class HttpXmlService implements XmlService {
    private final String baseUrl;
    private final HttpClient httpClient;

    public HttpXmlService(String baseUrl) {
        this.baseUrl = baseUrl;
        this.httpClient = HttpClient.newHttpClient();
    }

    @Override
    public String getXmlContent(String xmlId) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(baseUrl + "/xml/" + xmlId))
            .GET()
            .build();

        HttpResponse<String> response = httpClient.send(request, 
            HttpResponse.BodyHandlers.ofString());
        
        if (response.statusCode() == 200) {
            return response.body();
        }
        throw new RuntimeException("Failed to fetch XML: " + response.statusCode());
    }
}